package site.isscloud.jt809;

import java.io.*;

public class VehicleStaticInfo {
    private String plateNo;     // 车牌号(21字节)
    private byte plateColor;    // 车牌颜色(1字节)
    private byte vehicleType;   // 车辆类型(1字节)
    private String company;    // 所属企业(50字节)
    private String vin;        // VIN码(17字节)
    
    public VehicleStaticInfo(String plateNo, byte plateColor, byte vehicleType, 
                           String company, String vin) {
        this.plateNo = plateNo;
        this.plateColor = plateColor;
        this.vehicleType = vehicleType;
        this.company = company;
        this.vin = vin;
    }
    
    public byte[] toByteArray() throws UnsupportedEncodingException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
        // 车牌号(21字节)
        byte[] plateNoBytes = new byte[21];
        byte[] srcPlateNo = plateNo.getBytes("GBK");
        System.arraycopy(srcPlateNo, 0, plateNoBytes, 0, Math.min(srcPlateNo.length, 21));
        baos.write(plateNoBytes, 0, 21);
        
        // 车牌颜色(1字节)
        baos.write(plateColor);
        
        // 车辆类型(1字节)
        baos.write(vehicleType);
        
        // 所属企业(50字节)
        byte[] companyBytes = new byte[50];
        byte[] srcCompany = company.getBytes("GBK");
        System.arraycopy(srcCompany, 0, companyBytes, 0, Math.min(srcCompany.length, 50));
        baos.write(companyBytes, 0, 50);
        
        // VIN码(17字节)
        byte[] vinBytes = new byte[17];
        byte[] srcVin = vin.getBytes("GBK");
        System.arraycopy(srcVin, 0, vinBytes, 0, Math.min(srcVin.length, 17));
        baos.write(vinBytes, 0, 17);
        
        return baos.toByteArray();
    }
    
    public static VehicleStaticInfo fromByteArray(byte[] bytes) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        
        // 车牌号(21字节)
        byte[] plateNoBytes = new byte[21];
        bais.read(plateNoBytes);
        String plateNo = new String(plateNoBytes, "GBK").trim();
        
        // 车牌颜色(1字节)
        byte plateColor = (byte) bais.read();
        
        // 车辆类型(1字节)
        byte vehicleType = (byte) bais.read();
        
        // 所属企业(50字节)
        byte[] companyBytes = new byte[50];
        bais.read(companyBytes);
        String company = new String(companyBytes, "GBK").trim();
        
        // VIN码(17字节)
        byte[] vinBytes = new byte[17];
        bais.read(vinBytes);
        String vin = new String(vinBytes, "GBK").trim();
        
        return new VehicleStaticInfo(plateNo, plateColor, vehicleType, company, vin);
    }
    
    // Getter方法
    public String getPlateNo() { return plateNo; }
    public byte getPlateColor() { return plateColor; }
    public byte getVehicleType() { return vehicleType; }
    public String getCompany() { return company; }
    public String getVin() { return vin; }
    
    @Override
    public String toString() {
        return "VehicleStaticInfo{" +
               "plateNo='" + plateNo + '\'' +
               ", plateColor=" + plateColor +
               ", vehicleType=" + vehicleType +
               ", company='" + company + '\'' +
               ", vin='" + vin + '\'' +
               '}';
    }
}
